<?php 
function m($module_name) {
    static $ms = array();
    
    $module_name = trim($module_name);
    if (empty($module_name)) {
        tos_die('module name cannot be none');
    }
    
    if ( ! isset($ms[$module_name])) {
        $dir = MODULE_DIR . '/' . $module_name;
        $file = "$dir/$module_name.php";
        if ( ! file_exists($file)) {
            tos_die("cannot find module file $file");
        }
        
        include $file;
        
        $class_name = ucfirst($module_name);
        
        if ( ! class_exists($class_name)) {
            tos_die("$class_name doesn't exists");
        }
        
        $obj = new $class_name();
        $obj->set_name($module_name);
        $obj->set_dir($dir);
        $obj->init();
        
        $ms[$module_name] = $obj;
    }
    
    return $ms[$module_name];
}


$_tos_dbs = array(); 
function tos_add_db($name, $options) {
    global $_tos_dbs;
    
    $name = trim($name);
    if (empty($name)) {
        tos_die("db adapter name cannot be empty");
    }
    
    if ( ! isset($_tos_dbs[$name])) {
        $_tos_dbs[$name] = new Db_mysqli($options);
    } else {
        tos_die("db adapter $name already exists");
    }
    
    return true;
}


function tos_get_db($name = 'default') {
    global $_tos_dbs;
    
    $name = trim($name);
    if (empty($name)) {
        $name = 'default';
    }
    if (isset($_tos_dbs[$name])) {
        return $_tos_dbs[$name];
    } else {
        return false;
    }
}


function tos_http_get($key) {
    return isset($_GET[$key]) ? trim($_GET[$key]) : '';
}


function tos_http_post($key) {
    $v = isset($_POST[$key]) ? $_POST[$key] : '';
    if (is_string($v)) {
        $v = trim($v);
    }
    return $v;
}

function out() {
    static $out;
    if ( ! isset($out)) {
        $out = new View_out('out');
    }
    return $out;
}


$_tos_configs = array();
function tos_add_config($k, $v) {
    global $_tos_configs;
    if (isset($_tos_configs[$k])) {
        tos_die("Config $k has already been set.");
    } else {
        if (empty($v)) {
            tos_die("Config value for $k cannot be empty");
        }
        $_tos_configs[$k] = $v;
    }
}
function tos_set_config($k, $v) {
    global $_tos_configs;
    
    if (empty($v)) {
        tos_die("Config value for $k cannot be empty");
    }
    $_tos_configs[$k] = $v;
    
}
function tos_get_config($k) {
    global $_tos_configs;
    if ( ! isset($_tos_configs[$k])) {
        return false;
    } else {
        return $_tos_configs[$k];
    }
}


function tos_cdn_url($relative_url = '', $protocol = 'http') {
    static $base_url;
    if ( ! isset($base_url)) {
        $base_url = m('cdn')->get_base_url();
    }
    return $protocol . '://' . $base_url . '/' . $relative_url;
}


function tos_url($ps = array(), $is_new = true, $protocol = 'http') {
    static $site_base_url;
    if ( ! isset($site_base_url)) {
        $site_base_url = m('app')->get_base_url();
    }
    $arr = array();
    if ( ! $is_new) {
        $ps = array_merge($_GET, $ps);
    }
    foreach ($ps as $k => $v) {
        $k = trim($k);
        $v = trim($v);
        if ( ! empty($k) && ! empty($v)) {
            $arr[] = "$k=$v";
        }
    }
    $str = '';
    if ($arr)
        $str = "?" . implode('&', $arr);
    
    return $protocol . '://' . $site_base_url . $str;
}


function tos_html_strip($html, $length = 0) {
    $str = preg_replace('/<[^><]*>/i', '', $html);
    $str = preg_replace('/\s+/i', ' ', $str);
    $length = (int) $length;
    if ($length) {
        $pos = strpos($str, ' ', $length);
        if ($pos)
            $str = substr($str, 0, $pos) . '......';
    }
    return $str;
}


function tos_img($src, $width = 0, $height = 0, $attributes = array()) {
    static $cdn;
    if ( ! $cdn) $cdn = m('cdn');
    return $cdn->img($src, $width, $height, $attributes);
}


function tos_clean_url_segment($str) {
    $str = strtolower($str); 
    $str = preg_replace('/[^a-z0-9]+/i', '-', $str); // leave letters and numbers (separated by space) only 
    $str = trim($str, '-'); 
    return $str; 

}


function tos_clean_keyword($keyword) {
    $str = strtolower($keyword); 
    $str = preg_replace('/[^a-z0-9 ]+/i', '', $str);
    $str = preg_replace('/ +/i', ' ', $str);
    $str = trim($str);
    return $str;
}


function tos_explode_keywords($str, $delimiter = ',') {
    $str = strtolower($str);
    $delimiter = (string) $delimiter;
    //$delimiter = trim($delimiter);
    
    if (empty($delimiter)) {
        $delimiter = ',';
    }
    if ($delimiter === '_') {
        $delimiter = ',';
    }
    if ($delimiter === ' ') {
        $preg = '/[^a-z0-9_ ]/i';//delimiter 
    } else {
        $preg = '/[^a-z0-9_ ' . $delimiter . ']/i';
    }
    //echo $preg;
    $str = preg_replace($preg, '', $str);
    $str = trim($str, $delimiter);
    return explode($delimiter, $str);
}


function tos_die($message = '') {
    echo $message;
    tos_get_db()->trans_rollback();
    debug_print_backtrace();
    
    die();
}
?>